package 基础篇.工具类.BlockQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created by Administrator on 2018/7/29 0029.
 */
public class BlockQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<Integer> bq= new ArrayBlockingQueue<Integer>(2);
        new Producer(bq).start();
        new Producer(bq).start();
        new Consumer(bq).start();
    }
}

class Producer extends Thread{
    private BlockingQueue<Integer> blockingQueue;

    public Producer(BlockingQueue<Integer> blockingQueue){
        this.blockingQueue=blockingQueue;
    }

    public void run(){
        int i=0;
       while (true){
            try {
                i+=1;
                this.blockingQueue.put(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Consumer extends Thread{
    private BlockingQueue<Integer> blockingqeque;
    public Consumer(BlockingQueue<Integer> blockingqeque){
        this.blockingqeque=blockingqeque;
    }

    public void run(){
        while (true){
            try {
                this.blockingqeque.take();
                System.out.println(blockingqeque);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}